package idiet.wizards.page;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

public class WizardPageMealBasicInfo extends WizardPage {
	private static final Logger logger = Logger.getLogger(WizardPageMealBasicInfo.class);
	private static final String description = "\u8BF7\u8F93\u5165\u83DC\u80B4\u540D\u79F0\u3001\u522B\u540D\u3001\u5206\u7C7B\u3001\u529F\u6548\u3001\u7279\u70B9\u3001\u53E3\u5473\u3001\u5236\u4F5C\u6B65\u9AA4\u3001\u56FE\u7247\u3001\u5907\u6CE8\u7B49\u4FE1\u606F";
	private Text textMealName;
	private Text textMealAlias;
	private Text textFeature;
	private Text textEffect;
	private Text textMealOperation;
	private Text textComment;
	private Text textMealPicture;
	private Combo comboTaste;
	private Combo comboMealCategroy;
	//private CourseTasteService courseTasteService = (CourseTasteService) Activator.getApplicationContext().getBean(CourseTasteService.class);
	//private CourseCategoryService courseCategoryService = (CourseCategoryService) Activator.getApplicationContext().getBean(CourseCategoryService.class);
	//private CourseBasicInfo courseBasicInfo;//=new CourseBasicInfo();
	private Image imageCoursePicture; // = new Image(getShell().getDisplay(), "");
	private Hashtable<String, String> errors = new Hashtable<String, String>();
	private Canvas canvasCoursePicture;
	//private boolean isFirstOpen = true;

	/**
	 * Create the wizard.
	 */
	public WizardPageMealBasicInfo() {
		super("wizardPage");
		setPageComplete(true);
		// this.getNextPage();
		setTitle("\u6DFB\u52A0\u83DC\u80B4");
		setDescription(description);
		// this.setPageComplete(false);
		// this.
		errors.put("name", "请输入食物名称");
		errors.put("feature", "请输入食物特点");
		errors.put("effect", "请输入食物功效");
		errors.put("category", "请选择食物所属菜系");
		errors.put("taste", "请选择食物口味");
		errors.put("operation", "请输入食物制作过程");
	}

	@Override
	public IWizardPage getNextPage() {
		// TODO Auto-generated method stub
		// MessageDialog.openInformation(this.getShell(), "Open",
		// "Open Message Dialoglllllllllllllllllllkkkkkkkkkkkkkkkk!");
		//if (isFirstOpen) {
		//	isFirstOpen = false;
			//return super.getNextPage();
		//} else {
			if (this.textMealName.getText().trim().equals("")) {
				this.setErrorMessage(errors.get("name"));
				return this;
			}
			if (this.textFeature.getText().trim().equals("")) {
				this.setErrorMessage(errors.get("feature"));
				return this;
			}
			if (this.textEffect.getText().trim().equals("")) {
				this.setErrorMessage(errors.get("effect"));
				return this;
			}
			if (this.comboTaste.getText().trim().equals("")) {
				this.setErrorMessage(errors.get("taste"));
				return this;
			}
			if (this.comboMealCategroy.getText().trim().equals("")) {
				this.setErrorMessage(errors.get("category"));
				return this;
			}
			if (this.textMealOperation.getText().trim().equals("")) {
				this.setErrorMessage(errors.get("operation"));
				return this;
			}
			this.setErrorMessage(null);
			return super.getNextPage();
		//}
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);
		GridLayout gl_container = new GridLayout(3, false);
		gl_container.marginLeft = 50;
		gl_container.marginRight = 50;
		container.setLayout(gl_container);

		Label label = new Label(container, SWT.NONE);
		label.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label.setAlignment(SWT.RIGHT);
		label.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label.setText("菜名：");

		textMealName = new Text(container, SWT.BORDER);
		textMealName.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				/*Hashtable<String, String> result = Validator.InputValidator(
						"name", textMealName.getText());
				if (result.get("code").equals("F")) {
					setErrorMessage(errors.get(result.get("message")));
				} else {
					setErrorMessage(null);
				}*/
			}
		});
		textMealName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		final Point origin=new Point(0,0);
		canvasCoursePicture = new Canvas(container, SWT.H_SCROLL | SWT.V_SCROLL);
		canvasCoursePicture.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 6));
		final ScrollBar horizontalBar=canvasCoursePicture.getHorizontalBar();
		horizontalBar.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				if(imageCoursePicture!=null){
					int horizontalSelection=horizontalBar.getSelection();
					int destX=-horizontalSelection-origin.x;
					Rectangle rect=imageCoursePicture.getBounds();
					canvasCoursePicture.scroll(destX, 0, 0, 0, rect.width, rect.height, false);
					origin.x=-horizontalSelection;
					//canvasCoursePicture.redraw();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		final ScrollBar verticalBar=canvasCoursePicture.getVerticalBar();
		verticalBar.addSelectionListener(new SelectionListener(){

			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				if(imageCoursePicture!=null){
					int verticalSelection=verticalBar.getSelection();
					int destY=-verticalSelection-origin.y;
					Rectangle rect=imageCoursePicture.getBounds();
					canvasCoursePicture.scroll(0, destY, 0, 0, rect.width, rect.height, false);
					origin.y=-verticalSelection;
					//canvasCoursePicture.redraw();
				}
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
			
		});
		canvasCoursePicture.addControlListener(new ControlAdapter() {
			@Override
			public void controlResized(ControlEvent e) {
				if(imageCoursePicture!=null){
					Rectangle rect =imageCoursePicture.getBounds();
					Rectangle client=canvasCoursePicture.getClientArea();
					horizontalBar.setMaximum(rect.width);
					verticalBar.setMaximum(rect.height);
					horizontalBar.setThumb(Math.min(rect.width, client.width));
					verticalBar.setThumb(Math.min(rect.height, client.height));
					int horizontalPage=rect.width-client.width;
					int horizontalSelection=horizontalBar.getSelection();
					int verticalPage=rect.height-client.height;
					int verticalSelection=verticalBar.getSelection();
					if(horizontalSelection>=horizontalPage){
						if(horizontalPage<=0)
							horizontalSelection=0;
						origin.x=-horizontalSelection;
					}
					if(verticalSelection>=verticalPage){
						if(verticalPage<=0)
							verticalSelection=0;
						origin.y=-verticalSelection;
					}
					canvasCoursePicture.redraw();
				}
			}
		});
		canvasCoursePicture.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent e) {
				if(imageCoursePicture!=null){
					GC gc=e.gc;
					gc.drawImage(imageCoursePicture, origin.x,origin.y);
					Rectangle rect=imageCoursePicture.getBounds();
					Rectangle client=canvasCoursePicture.getClientArea();
					int marginWidth=client.width-rect.width;
					if(marginWidth>0){
						gc.fillRectangle(rect.width, 0, marginWidth, client.height);
					}
					int marginHeight=client.height-rect.height;
					if(marginHeight>0){
						gc.fillRectangle(0, rect.height, client.width, marginHeight);
					}
				/*if(imageCoursePicture!=null)
					e.gc.drawImage(imageCoursePicture, 0, 0);*/
				}
			}
		});
		canvasCoursePicture.setLayout(new FillLayout(SWT.HORIZONTAL));

		Label label_1 = new Label(container, SWT.NONE);
		label_1.setAlignment(SWT.RIGHT);
		label_1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_1.setText("\u522B\u540D\uFF1A");

		textMealAlias = new Text(container, SWT.BORDER);
		textMealAlias.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));

		Label label_2 = new Label(container, SWT.NONE);
		label_2.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label_2.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1,
				1));
		label_2.setAlignment(SWT.RIGHT);
		label_2.setText("\u7279\u70B9\uFF1A");

		textFeature = new Text(container, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL
				| SWT.MULTI);
		textFeature.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				/*Hashtable<String, String> result = Validator.InputValidator(
						"feature", textFeature.getText());
				if (result.get("code").equals("F")) {
					setErrorMessage(errors.get(result.get("message")));
				} else {
					setErrorMessage(null);
				}*/
			}
		});
		GridData gd_textFeature = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1);
		gd_textFeature.heightHint = 51;
		textFeature.setLayoutData(gd_textFeature);

		Label label_3 = new Label(container, SWT.NONE);
		label_3.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label_3.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1,
				1));
		label_3.setAlignment(SWT.RIGHT);
		label_3.setText("\u529F\u6548\uFF1A");

		textEffect = new Text(container, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL
				| SWT.MULTI);
		textEffect.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				/*Hashtable<String, String> result = Validator.InputValidator(
						"effect", textEffect.getText());
				if (result.get("code").equals("F")) {
					setErrorMessage(errors.get(result.get("message")));
				} else {
					setErrorMessage(null);
				}*/
			}
		});
		GridData gd_textEffect = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1);
		gd_textEffect.heightHint = 53;
		textEffect.setLayoutData(gd_textEffect);

		Label label_4 = new Label(container, SWT.NONE);
		label_4.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label_4.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_4.setAlignment(SWT.RIGHT);
		label_4.setText("\u53E3\u5473\uFF1A");

		comboTaste = new Combo(container, SWT.READ_ONLY);
		comboTaste.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				/*Hashtable<String, String> result = Validator.InputValidator(
						"taste", comboTaste.getText());
				if (result.get("code").equals("F")) {
					setErrorMessage(errors.get(result.get("message")));
				} else {
					setErrorMessage(null);
				}*/
			}
		});
		comboTaste.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		//comboTaste.setItems(courseTasteService.getCourseTastes().toArray(
		//		new String[0]));

		Label label_5 = new Label(container, SWT.NONE);
		label_5.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_5.setAlignment(SWT.RIGHT);
		label_5.setText("所属菜系：");

		comboMealCategroy = new Combo(container, SWT.READ_ONLY);
		comboMealCategroy.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				/*Hashtable<String, String> result = Validator.InputValidator(
						"category", comboMealCategroy.getText());
				if (result.get("code").equals("F")) {
					setErrorMessage(errors.get(result.get("message")));
				} else {
					setErrorMessage(null);
				}*/
			}
		});
		comboMealCategroy.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 1, 1));
		/*comboMealCategroy.setItems(courseCategoryService.getCourseCategories()
				.toArray(new String[0]));*/

		Label label_6 = new Label(container, SWT.NONE);
		label_6.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false,
				1, 1));
		label_6.setAlignment(SWT.RIGHT);
		label_6.setText("\u56FE\u7247\uFF1A");

		textMealPicture = new Text(container, SWT.BORDER);
		GridData gd_textMealPicture = new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1);
		gd_textMealPicture.widthHint = 77;
		textMealPicture.setLayoutData(gd_textMealPicture);

		Button btnUploadMealPicture = new Button(container, SWT.NONE);
		btnUploadMealPicture.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				FileDialog fd = new FileDialog(getShell(), SWT.OPEN);
				textMealPicture.setText("");
				String fileName=fd.open();
				textMealPicture.setText(fileName==null?"":fileName);
				File file=new File(textMealPicture.getText());
				if(!textMealPicture.getText().equals("")){
					if(file.exists()){
						imageCoursePicture = new Image(getShell().getDisplay(),textMealPicture.getText()); 
						canvasCoursePicture.redraw();
					}else{
						MessageDialog.openWarning(getShell(), "菜肴图片提醒", "图片找不到，请确认图片选择是否正确！");
					}
				}
			}
		});
		btnUploadMealPicture.setText("    \u6D4F  \u89C8    ");

		Label label_7 = new Label(container, SWT.NONE);
		label_7.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
		label_7.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1,
				1));
		label_7.setAlignment(SWT.RIGHT);
		label_7.setText("\u5236\u4F5C\u8FC7\u7A0B\uFF1A");

		textMealOperation = new Text(container, SWT.BORDER | SWT.WRAP
				| SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
		textMealOperation.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				/*Hashtable<String, String> result = Validator.InputValidator(
						"operation", textMealOperation.getText());
				if (result.get("code").equals("F")) {
					setErrorMessage(errors.get(result.get("message")));
				} else {
					setErrorMessage(null);
				}*/
			}
		});
		GridData gd_textMealOperation = new GridData(SWT.FILL, SWT.CENTER,true, false, 2, 1);
		gd_textMealOperation.heightHint = 84;
		gd_textMealOperation.widthHint = 478;
		textMealOperation.setLayoutData(gd_textMealOperation);

		Label label_8 = new Label(container, SWT.NONE);
		label_8.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1,1));
		label_8.setAlignment(SWT.RIGHT);
		label_8.setText("\u5907\u6CE8\uFF1A");

		textComment = new Text(container, SWT.BORDER | SWT.V_SCROLL);
		GridData gd_textComment = new GridData(SWT.FILL, SWT.CENTER, true,false, 2, 1);
		gd_textComment.heightHint = 54;
		textComment.setLayoutData(gd_textComment);
	}

	/*public CourseBasicInfo getCourseBasicInfo() throws IOException {
		this.courseBasicInfo = new CourseBasicInfo();
		this.courseBasicInfo.setCourseId(UUID.randomUUID().toString());
		this.courseBasicInfo.setCourseName(this.textMealName.getText());
		this.courseBasicInfo.setCourseAlias(this.textMealAlias.getText());
		this.courseBasicInfo.setEffect(this.textEffect.getText());
		this.courseBasicInfo.setFeature(this.textFeature.getText());
		this.courseBasicInfo
				.setCourseCategory(this.comboMealCategroy.getText());
		this.courseBasicInfo.setOperation(this.textMealOperation.getText());
		this.courseBasicInfo.setCourseTaste(this.comboTaste.getText());
		String imgPath = this.textMealPicture.getText();
		if (imgPath != null && !imgPath.trim().equals("")) {
			File file=new File(imgPath);
			if(file.exists()){
				ByteArrayOutputStream out=new ByteArrayOutputStream();
				ImageLoader imageLoader=new ImageLoader();
				ImageData[] imageData=imageLoader.load(imgPath);//=new ImageData[]{new ImageData(imgPath)};
				imageLoader.save(out, imageData[0].type);
				this.courseBasicInfo.setCoursePicture(out.toByteArray());
			}else{
				this.courseBasicInfo.setCoursePicture(null);
			}
		} else {
			this.courseBasicInfo.setCoursePicture(null);
		}

		this.courseBasicInfo.setComment(this.textComment.getText());
		return courseBasicInfo;
	}*/
}
